<?php 
set_time_limit(0);ini_set('max_execution_time', 0);
define("TIME", "Y-m-d H:i:s");
define("ROOT_PATH", realpath(dirname(__FILE__))."/../");
//日志相关
define("LOG_DIR", ROOT_PATH . DIRECTORY_SEPARATOR . "log" . DIRECTORY_SEPARATOR);
define("LOG_FILE_FORMAT", "Y-m-d");
date_default_timezone_set ( 'asia/shanghai' );

/**
 * 写日志
 * 
 * 时间是产生的微秒时间
 * 
 * @param string $v 日志内容
 * @param string $level 等级
 */
function write_log($v="", $level="INFO"){
	$fp = fopen(LOG_DIR."log.".date(LOG_FILE_FORMAT).".txt", "a");
	if(!$fp){
		return false;
	}else{
		$v = time_auto(6). " " . $level . ":" . $v;
		//$v = time_auto(6). " " . $level . ":" . $v . "[url:" . $_SERVER['REQUEST_URI'] . ",ip:" . getip() . "]";
		
		fwrite($fp, $v."\r\n");
		fclose($fp);
		return true;
	}
}
/**
 * 写日志_结果日志
 * 
 * 时间是产生的微秒时间
 * 
 * @param string $v 日志内容
 * @param string $level 等级
 */
function write_log_ok($v="", $level="INFO"){
	$fp = fopen(LOG_DIR."log.".date(LOG_FILE_FORMAT)."_ok.txt", "a");
	if(!$fp){
		return false;
	}else{
		fwrite($fp, $v."\r\n");
		fclose($fp);
		return true;
	}
}

/**
 * 将字符串保存到文件里
 * @param String $str 保存的字符
 * @param String $filename 文件名
 */
function saveFile($str, $filename) {
	$fp = fopen ( $filename, "w+" );
	if ($fp == NULL)
		echo "文件处理出错";
	
	fwrite ( $fp, $str ); //写入2个字符 
	fclose ( $fp );
	return true;
}

/**
 * 读取文件所有内容
 * @param String $filename 文件名
 */
function readFileContent($filename){
//	$fn = "2.dat";
	$rh = fopen ( $filename, "rb" );
	$pb = fread ( $rh, filesize ( $filename ) );
	fclose ( $rh );
	return $pb;
}

/**
 * post请求页面
 * $url:请求的url
 * $params:post值,可以是数据,也可以是a=1&b=2
 * $encoding:是否要转为utf8
 * $type:是post还是get
 */
function curl($url,$params=null,$encoding=false,$type=true){
	$ch = curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 0);
	if($type)
		curl_setopt($ch, CURLOPT_POST, 1);//post
	if(!empty($params))
		curl_setopt($ch, CURLOPT_POSTFIELDS, $params);
	$ip = ipRand();
	//请求头信息
	$headers = array(
//		"Accept:text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
//		"Accept-Encoding:gzip, deflate",
		"Accept-Language:zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3",
		"Connection:keep-alive",
//		"Cookie:LastUrl=; FirstOKURL=http%3A//www.okooo.com/match/1/164185/history/; First_Source=www.okooo.com; __utma=56961525.1733537133.1348810154.1348810154.1348810154.1; __utmz=56961525.1348810154.1.1.utmcsr=(direct)|utmccn=(direct)|utmcmd=(none)",
//		"Host:www.okooo.com",
		"x-insight:activate",
		"User-Agent:Mozilla/5.0 (Windows NT 6.1; rv:15.0) Gecko/20100101 Firefox/15.0.1 FirePHP/0.7.1",
		"CLIENT-IP:{$ip}",
		"X-FORWARDED-FOR:{$ip}",
	);
	curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
	$output = curl_exec($ch);
	curl_close($ch);
	
	if($encoding)
		return iconv("GBK", "UTF-8", $output);
	else
		return $output;
}
/**
 * 随机产生ip
 */
function ipRand(){
	return rand(1, 244).".".rand(1, 244).".".rand(1, 244).".".rand(1, 244);
}

/**
 * 自动产生一个张微时间_可先再加随机数
 */
function time_auto($l=8,$rand=false){
	$temp_time = microtime(true);
	return date("Y-m-d H i s")."_".substr(($temp_time-floor($temp_time)), 2, $l).(($rand)?("_".rand(100,999)):"");
		
}

static $_client_real_ip = null;
/**
 * 得到客户端ip
 */
function getip(){
	static $_client_real_ip;
	if(null === $_client_real_ip){
		if (isset($_SERVER)){
			if(isset($_SERVER['HTTP_X_FORWARDED_FOR'])){
				$_client_real_ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
			}elseif(isset($_SERVER['HTTP_CLIENT_IP'])){
				$_client_real_ip = $_SERVER['HTTP_CLIENT_IP'];
			}else{
				$_client_real_ip = $_SERVER['REMOTE_ADDR'];
			}
		}else{
			if(getenv("HTTP_X_FORWARDED_FOR")){
				$_client_real_ip = getenv( "HTTP_X_FORWARDED_FOR");
			}elseif(getenv("HTTP_CLIENT_IP")){
				$_client_real_ip = getenv("HTTP_CLIENT_IP");
			}else{
				$_client_real_ip = getenv("REMOTE_ADDR");
			}
		}
		return (preg_match('/\A((([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\.){3}(([0-9]?[0-9])|(1[0-9]{2})|(2[0-4][0-9])|(25[0-5]))\Z/',$_client_real_ip))?$_client_real_ip:0;
	}else{
		return $_client_real_ip;
	}
}
/**
 * 从域名中提取用户名
 * $v:域名
 */
function domain_name_get_user_name($v){
	if(empty($v))return;

	$domains =array(
		"com",
		"net",
		"biz",
		"org",
		"au",
		"tw",
		"info",
		"jk",
		"uk",
		"web",
		"id",
		"edu",
		"co",
		"za",
		"je",
		"ca",
		"ul",
		"nl",
		"eu",
		"us",
		"se",
	);
	foreach ($domains as $domain) {
		$v = preg_replace("/\.{$domain}/", "", $v);
	}
	$v = preg_replace('/www./', "", $v);
	$v = preg_replace('/\./', "", $v);
	
	//去除"/"后面的把的字符
	$v = explode("/", $v);
	return $v[0];
}

